package hongcaosp.app.android.user.settings.safety.update;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import hongcaosp.app.android.R;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.BaseResponse;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.impl.PasswordModleImpl;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.toastcompat.ToastManager;

public class UpdatePasswordFragment extends BaseFragment {

    private EditText et_password,et_password_again;


    @Override
    public void initView() {
        et_password = findViewById(R.id.et_password);
        et_password_again = findViewById(R.id.et_password_again);

        findViewById(R.id.btn_next).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit(et_password.getText().toString().trim(),et_password_again.getText().toString().trim());
            }
        });
    }

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_update_password;
    }

    private void submit(String password,String passwordAgain) {
        if (TextUtils.isEmpty(password)) {
            ToastManager.getInstance().showToast("请输入新密码");
            return;
        }
        if (password.length() < 8) {
            ToastManager.getInstance().showToast("密码长度不能小于8位噢");
            return;
        }
        if (password.length() > 20) {
            ToastManager.getInstance().showToast("密码长度至多20位噢");
            return;
        }
        if (TextUtils.isEmpty(passwordAgain)) {
            ToastManager.getInstance().showToast("请再次输入新密码");
            return;
        }
        if (!password.equals(passwordAgain)) {
            ToastManager.getInstance().showToast("两次密码输入不一致");
            return;
        }
        new PasswordModleImpl().updatePassWordNew(UserToken.getDefault().getToken(), password, passwordAgain, new DataCallBack<BaseResponse>() {
            @Override
            public void onGetData(BaseResponse data) {
                super.onGetData(data);
                ToastManager.getInstance().showToast("密码修改成功");
                getActivity().finish();
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });
    }
}
